package manager;

import java.io.Serializable;
import java.util.Date;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import utils.ResticketDAORemote;
import utils.UserDAORemote;
import utils.UsertypeDAORemote;
import dtos.ResticketDTO;
import dtos.UserDTO;

@ManagedBean(name = "inactiveManager")
@SessionScoped
public class InactiveBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7177339879521069812L;
	String selected;

	@EJB
	UserDAORemote ur;

	@EJB
	UsertypeDAORemote utr;

	@EJB
	ResticketDAORemote rr;

	UserDTO currentUser;

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());

	private String confStatusmsg;
	private String msgStatusChanged = bundle
			.getString("Beans_manager_inactiveBean_msgStatusChanged");
	private String msgToInactiveYouGetNotif = bundle
			.getString("Beans_manager_inactiveBean_msgToInactiveYouGetNotif");

	public String getSelected() {
		return selected;
	}

	public boolean isom;

	public void setSelected(String selected) {
		this.selected = selected;
	}

	public String getConfStatusmsg() {
		return confStatusmsg;
	}

	public void setConfStatusmsg(String confStatusmsg) {
		this.confStatusmsg = confStatusmsg;
	}

	@PostConstruct
	public void init() {
		// HttpSession session = (HttpSession)
		// FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		// currentUser = (UserDTO)session.getAttribute("user");
		currentUser = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("loggedUser");
		this.isom = ur.isOfficeManager(currentUser);
		if (currentUser.getIsActive() == 1) {
			this.selected = "activ";
		} else {
			this.selected = "inactiv";
		}
		confStatusmsg = "";
	}

	/**
	 * @author Loredana Pop
	 * @version 1.0 Changes the status of logged in Office mananger, if he
	 *          becomes inactive the admin will become responsible for his
	 *          notifications.
	 */
	public void redirectAdmin() {
		UserDTO u = currentUser;
		if (this.selected.equals("inactiv")) {
			UserDTO admin = ur.getAdmin();
			this.selected = "inactiv";
			u.setIsActive(0);
			u.setIdUser(admin.getId());

			ur.insertUser(u);

			ResticketDTO ticket = new ResticketDTO();
			ticket.setIdUser(u.getId());
			ticket.setDescription("Office Manager " + u.getUsername()
					+ msgToInactiveYouGetNotif);
			ticket.setStatus("Pending");
			ticket.setDate(new Date());
			ticket.setQuantity(1);
			rr.insertResticket(ticket);
		} else {
			this.selected = "activ";
			u.setIsActive(1);
			u.setIdUser(0);
			ur.insertUser(u);
		}
		confStatusmsg = msgStatusChanged;
	}

	public boolean isIsom() {
		return isom;
	}

	public void setIsom(boolean isom) {
		this.isom = isom;
	}

	public UserDTO getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(UserDTO currentUser) {
		this.currentUser = currentUser;
	}

	public String getMsgToInactiveYouGetNotif() {
		return msgToInactiveYouGetNotif;
	}

	public void setMsgToInactiveYouGetNotif(String msgToInactiveYouGetNotif) {
		this.msgToInactiveYouGetNotif = msgToInactiveYouGetNotif;
	}

	public String getMsgStatusChanged() {
		return msgStatusChanged;
	}

	public void setMsgStatusChanged(String msgStatusChanged) {
		this.msgStatusChanged = msgStatusChanged;
	}

}
